﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XslPad.Core;
using XslPad.Core.Managers;
using ICSharpCode.AvalonEdit.CodeCompletion;
using XslPad.WPF.Completions;
using ICSharpCode.AvalonEdit.Editing;
using System.Xml.Linq;

namespace XslPad.WPF
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private CompletionWindow _xslCompletionWindow = null;

        public MainWindow()
        {
            InitializeComponent();
            this.xslTextEditor.Text = XslPadConstants.DefaultXsl;

            InitializeXslEditor();
        }

        private void InitializeXslEditor()
        {
            this.xslTextEditor.TextArea.TextEntered += new TextCompositionEventHandler(TextArea_TextEntered);
        }

        void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
        {
            if (e.Text == ":")
            {
                int index = ((TextArea)e.Source).Caret.Offset;
                string last4Chars = ((TextArea)e.Source).Document.Text.Substring(index - 5, 5);

                if ("<xsl:".Equals(last4Chars, StringComparison.InvariantCultureIgnoreCase))
                {
                    _xslCompletionWindow = new CompletionWindow(this.xslTextEditor.TextArea);

                    IList<ICompletionData> data = _xslCompletionWindow.CompletionList.CompletionData;
                    data.Add(new XslCompletionData("include href=\"\"/>"));
                    data.Add(new XslCompletionData("key  name=\"\" match=\"\" use=\"\"/>"));
                    data.Add(new XslCompletionData("output version=\"\" encoding=\"\"/>"));
                    data.Add(new XslCompletionData("param name=\"\" select=\"\"/>"));

                    // Template
                    data.Add(new XslCompletionData("template match=\"\">" + Environment.NewLine + "</xsl:template>"));
                    data.Add(new XslCompletionData("template match=\"\" mode=\"\">" + Environment.NewLine + "</xsl:template>"));
                    data.Add(new XslCompletionData("template name=\"\" match=\"\" mode=\"\">" + Environment.NewLine + "</xsl:template>"));

                    // ApplyTemplates
                    data.Add(new XslCompletionData("apply-templates select=\"\"/>"));
                    data.Add(new XslCompletionData("apply-templates select=\"\" mode=\"\"/>"));

                    data.Add(new XslCompletionData("attribute name=\"\"></xsl:attribute>"));
                    data.Add(new XslCompletionData("call-template name=\"\"/>"));

                    data.Add(new XslCompletionData("choose>" + Environment.NewLine +
                                                        "<xsl:when test=\"\">" + Environment.NewLine +
                                                        "</xsl:when>" + Environment.NewLine +
                                                        "<xsl:otherwise>" + Environment.NewLine +
                                                        "</xsl:otherwise>" + Environment.NewLine +
                                                    "</xsl:choose>"));

                    data.Add(new XslCompletionData("comment></xsl:comment>"));
                    data.Add(new XslCompletionData("copy>" + Environment.NewLine +
                                                    "</xsl:copy>"));
                    data.Add(new XslCompletionData("copy-of select=\"\"/>"));
                    data.Add(new XslCompletionData("element name=\"\">" + Environment.NewLine + "</xsl:element>"));
                    data.Add(new XslCompletionData("for-each select=\"\">" + Environment.NewLine + "</xsl:for-each>"));
                    data.Add(new XslCompletionData("if test=\"\">" + Environment.NewLine + "</xsl:if>"));
                    data.Add(new XslCompletionData("text></xsl:text>"));
                    data.Add(new XslCompletionData("value-of select=\"\"/>"));
                    data.Add(new XslCompletionData("variable name=\"\" select=\"\"/>"));

                    _xslCompletionWindow.Width = 300;
                    _xslCompletionWindow.Show();
                    _xslCompletionWindow.Closed += delegate
                    {
                        _xslCompletionWindow = null;
                    };
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            resultTextEditor.Text = BasicXslTransformer.Transform(xmlTextEditor.Text, xslTextEditor.Text);
        }

        private void Format_Click(object sender, RoutedEventArgs e)
        {
            // Source XML
            xmlTextEditor.Text = XDocument.Parse(xmlTextEditor.Text).ToString();

            // Source XSL
            xslTextEditor.Text = XDocument.Parse(xslTextEditor.Text).ToString();
        }
    }
}
